import { Component, OnInit } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { Router, ActivatedRoute } from '@angular/router' //路由引入
import { config } from '../../shared/smartadmin.config'

declare var $: any
@Component({
  selector: 'sa-project-cost',
  templateUrl: './project-cost.component.html'
})
export class ProjectCostComponent implements OnInit {
  tableData: any[] = [] //table数据
  totalCount: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数
  tableParam: any = {
    selectedCompanyId: '',
    selectedCompanyName: '',
    startTime: '',
    endTime: '',
    dateSortType: "0",
    createUserName: ''
  }
  carList: any[] = []
  isShowDelete = false // 默认不显示删除按钮
  constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
    activatedRoute.queryParams.subscribe(queryParams => {
      this.curPage = queryParams.nowPage || 1
    })
  }
  ngOnInit() {
    this.load()
  }
  //table刷新
  refresh() {
    this.curPage = 1
    this.load()
  }
  //表格搜索
  tableSearch() {
    this.curPage = 1
    this.load()
  }
  //分页回调
  paginate(e) {
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }
  //初始化加载list数据
  load() {
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        projectId: this.tableParam.selectedCompanyId,
        startTime: this.tableParam.startTime ? this.tableParam.startTime + ' 00:00:00' : '',
        endTime: this.tableParam.endTime ? this.tableParam.endTime + ' 23:59:59' : '',
        createUserName: this.tableParam.createUserName,
        dateSortType: this.tableParam.dateSortType
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(data)
    this.jsonApiService.http('projectCost/projectCostList.html', reqdata).subscribe(data => {
      if (data.isSuccess) {
        for (let i = 0; i < data.exhibitDatas.length; i++) {
          let dataValue = data.exhibitDatas[i].date.split(' ');
          data.exhibitDatas[i].dataValue = dataValue[0]
        }
        this.tableData = JSON.parse(JSON.stringify(data.exhibitDatas))
        this.totalCount = data.recordCount
      } else {
        this.jsonApiService.message(data.message)
      }
    })
  }
  // 切换倒序升序
  columnSort(type) {
    this.tableParam.dateSortType = type.toString()
    this.curPage = 1
    this.load()
  }
  // 数据导出
  exportData() {
    if (!this.tableParam.startTime || !this.tableParam.endTime) {
      this.jsonApiService.message("请先选择时间区间再进行导出")
      return
    }
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        projectId: this.tableParam.selectedCompanyId,
        startTime: this.tableParam.startTime ? this.tableParam.startTime + ' 00:00:00' : '',
        endTime: this.tableParam.endTime ? this.tableParam.endTime + ' 23:59:59' : '',
        createUserName: this.tableParam.createUserName,
        dateSortType: this.tableParam.dateSortType
      },
      isExport: 1
    }
    common.exprotDataFile(data, 'gridPager', config.BaseURL + 'projectCost/projectCostList.html')
  }

  //公司选中回调
  companySelected(data) {
    this.tableParam.selectedCompanyId = data.companyId
    this.tableParam.selectedCompanyName = data.companyName
  }

  //select2 格式回调函数
  formatRepo(state) {
    if (!state.id) {
      return state.text
    }
    var $state = $('<span><i class="fa fa-car green"></i>&nbsp;&nbsp;' + state.text + '</span>')
    return $state
  }
  // 删除
  deleteRow(row) {
    this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除?').then(() => {
      let reqdata = 'id=' + row.id
      this.jsonApiService.http('projectCost/delProjectCost.html', reqdata).subscribe(data => {
        this.jsonApiService.message(data.message)
        this.load()
      })
    })
  }
  // 接收子组件传递过来的数据
  getSelectTableTime(time) {
    this.tableParam.startTime = time.startTime
    this.tableParam.endTime = time.endTime
  }

  //查看
  ProblemModalTableData: any = ''
  tablelook(row, modal) {
    this.ProblemModalTableData = row.exceptionDescription
    modal.show()
  }
  cancelProblemModal(modal) {
    this.ProblemModalTableData = ''
    modal.hide()
  }
  // 删除输入
  delKey() {
    this.tableParam.createUserName = ''
  }
}
